import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/person/post'

const api = {

    /**
     * 新增岗位与组织关系
     * @param post
     * @returns {Promise<*|undefined>}
     */
    createPost: (post) =>
      axiosClient().post(`${requestUrl}/addInfo`, post),

    /**
     * 根据ID删除岗位与组织关系
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deletePostById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新岗位与组织关系
     * @param id
     * @param post
     * @returns {Promise<*|undefined>}
     */
    updatePostFull: (id, post) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, post),

    /**
     * 根据ID部分更新岗位与组织关系
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updatePostPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询岗位与组织关系
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getPostById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询岗位与组织关系
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getPostPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询岗位与组织关系
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getPostList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.postDefId) {
            params.append('postDefId', businessParam.postDefId)
        }
        if (businessParam.orgId) {
            params.append('orgId', businessParam.orgId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
